package com.ecarx.vhaltests.hy11.testcases.climate;

import android.car.VehicleAreaSeat;
import android.car.VehiclePropertyIds;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.CarPropertyManager;
import android.util.Log;

import com.ecarx.vhaltests.IpcpClient;
import com.ecarx.vhaltests.VhalUnitTestBase;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;


@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class HvacRecircOnTest extends VhalUnitTestBase
{
    private static final String TAG = "HvacRecircOnTest";
    private static boolean halValue;
    private static int halStatus;
    private static int halArea;

    private static final int HVAC_RECIRC_ON = VehiclePropertyIds.HVAC_RECIRC_ON;
    private static final int HVAC_RECIRC_ON_AREA = VehicleAreaSeat.SEAT_ROW_1_LEFT | VehicleAreaSeat.SEAT_ROW_1_RIGHT | VehicleAreaSeat.SEAT_ROW_2_LEFT
            | VehicleAreaSeat.SEAT_ROW_2_CENTER | VehicleAreaSeat.SEAT_ROW_2_RIGHT;

    private static final int SERVICE_ID_CLIMATE = 112;
    private static final int OP_ID_CLIMATE = 0xc8;

    final static  Object lock = new Object();
    static IpcpClient ipcpClient = new IpcpClient();

    @BeforeClass
    public static void testInit() {
        mCarPropertyMgr.registerCallback(carPropertyEventCallback, HVAC_RECIRC_ON, CarPropertyManager.SENSOR_RATE_ONCHANGE);
        setConfig(OP_ID_CLIMATE);
    }

    @Test  //循环模式有效性测试
    public void test01_HvacRecircOnAvail() {
        PrepareHvacRecircOnTestEnv(1,0);
        delay();
        HvacRecircOnAvail(true);
    }

    @Test  //循环模式无效性测试
    public void test02_HvacRecircOnNotAvail() {
        PrepareHvacRecircOnTestEnv(3,0);
        delay();
        HvacRecircOnAvail(false);
    }

    @Test  //外循环
    public void test03_HvacRecircSetAut() {
        PrepareHvacRecircOnTestEnv(1,0);
        delay();
        HvacRecircOnSet(false);
    }

    @Test  //内循环
    public void test04_HvacRecircSetRecircFull() {
        PrepareHvacRecircOnTestEnv(1,1);
        delay();
        HvacRecircOnSet(true);
    }

    private static  CarPropertyManager.CarPropertyEventCallback carPropertyEventCallback = new CarPropertyManager.CarPropertyEventCallback() {
        @Override
        public void onChangeEvent(CarPropertyValue carPropertyValue) {
            try {
                halValue = (boolean) carPropertyValue.getValue();
                halStatus = carPropertyValue.getStatus();
                halArea = carPropertyValue.getAreaId();
                Log.d(TAG, "HVAC_RECIRC_ON onChangeEvent value: propertyId ：" + carPropertyValue.getPropertyId() + " propertyValue：" + carPropertyValue.getValue() + " status : " + carPropertyValue.getStatus());
                synchronized (lock) {
                    lock.notify();
                }
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }

        @Override
        public void onErrorEvent(int i, int i1) {
        }
    };

    public void lockWait() {
        try {
            synchronized (lock) {
                lock.wait(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void HvacRecircOnAvail( boolean mode) {
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(HVAC_RECIRC_ON, HVAC_RECIRC_ON_AREA);
        Log.d(TAG, "HvacRecircOnAvail  isAvail：" + isAvail + " mode： " + mode);
        Assert.assertEquals (mode, isAvail);
    }

    private void HvacRecircOnSet(boolean mode ) {
        boolean bRet = false;
        Log.d(TAG, "HvacRecircOnSet set mode ：" + mode);
        mCarPropertyMgr.setBooleanProperty(HVAC_RECIRC_ON, HVAC_RECIRC_ON_AREA, mode);
        delay();
        bRet = mCarPropertyMgr.getBooleanProperty(HVAC_RECIRC_ON, HVAC_RECIRC_ON_AREA);
        Assert.assertEquals( mode , bRet);
        lockWait();
        Assert.assertEquals(mode,halValue);
    }

    private void PrepareHvacRecircOnTestEnv(int avail, int onOff) {
        Log.d(TAG, " PrepareHvacRecircOnTestEnv avail：" + avail + " onOff :" + onOff);
        String  availabilityIndex = "paclrecirc.availability";
        String dataIndex = "paclrecirc.data";
        String[] config_indexs = {availabilityIndex, dataIndex};
        int[] values = {avail, onOff};
        ipcpClient.setClimaSignal(SERVICE_ID_CLIMATE, OP_ID_CLIMATE, config_indexs,  values);
    }

    private static void setConfig(int OperId) {
        int climateOperId[] = {OperId};
        ipcpClient.setConfig(SERVICE_ID_CLIMATE, climateOperId);
    }
}
